using System.Text.RegularExpressions;

namespace FileRead;


public class MarkdownFile
{
    private readonly string notePath;

    public MarkdownFile(string notePath)
    {
        this.notePath = notePath;
    }

    public List<string> GetDstFilePath()
    {
        var dstPaths = this.GetDstDirByLinqReg();
        foreach (var path in dstPaths)
        {
            Console.WriteLine(path);
            var mdPaths = Directory.GetFiles(path);
            foreach (var mdPath in mdPaths)
            {
                System.Console.WriteLine(mdPath);
                System.Console.WriteLine(new FileInfo(mdPath).LastWriteTime);
            }



            break;
        }

        return dstPaths;

    }

    public List<string> GetDstFilePathLinq()
    {

        var dstPaths = this.GetDstDirByLinqReg();
        var dstFilePath = new List<string>();
        foreach (var path in dstPaths)
        {
            System.Console.WriteLine(path);
            var files = GetDstFilePathByFolder(path);
            foreach (var file in files)
            {
                dstFilePath.Add(file);
                // System.Console.WriteLine(file);
                // System.Console.WriteLine(Path.GetExtension(file));
                // System.Console.WriteLine(Path.GetExtension(file).Contains(".txt"));
                // System.Console.WriteLine(Path.GetExtension(file).Contains(".md"));
            }
        }
        return dstFilePath;
    }

    public static List<string> GetDstFilePathByFolder(string folderPath)
    {

        var files = new DirectoryInfo(folderPath).GetFiles()
                        .OrderByDescending(f => f.LastWriteTime)
                        .Select(f => f.FullName)
                        .Where((file)=>!file.Contains(".DS_Store"));

        return files.ToList();

    }
    private List<string> GetDstDir()
    {
        string[] directories = Directory.GetDirectories(this.notePath);
        var dstPaths = new List<string>();

        foreach (string directory in directories)
        {
            var dstDirName = directory.Split("/").Last();
            if (dstDirName.Contains('_'))
            {
                // C#中如何向数组中添加元素
                dstPaths.Add(directory);
            }
        }
        return directories.ToList();

    }
    private List<string> GetDstDirLinq()
    {
        var dstPaths = Directory.GetDirectories(this.notePath)
                                .Where(directory => directory.Split("/").Last().Contains('_'))
                                .ToList();
        return dstPaths;
    }

    public List<string> GetDstDirByLinqReg()
    {

        var dstPaths = Directory
            .GetDirectories(notePath, "*", SearchOption.TopDirectoryOnly)
            .Where(directory =>
                Regex.IsMatch(Path.GetFileName(directory), ".{2}_"))
            .ToList();

        return dstPaths;
    }



}